Using the Text Block nodes

Use the Text Block nodes to show a small amount of text in your application. Text block node uses the font you select to render the text. Use Text Block 3D to show text in 3D space and Text Block 2D to show text in 2D space.

The default font in Kanzi is Fira Sans Regular. It is stored in <KanziInstallation>/Studio/Asset Library/Fonts. When you want to use your own font, import it to your Kanzi Studio project. See Importing fonts.

Note that in order to properly render text content you have to use a font that includes the glyphs of the languages you want to include in your Kanzi application.

By default the Text Block 2D nodes use a Style which sets the Snap to Pixel property. The Snap to Pixel property rounds the translation and scale of the Text Block 2D node to a full pixel, which makes it easier to position the node pixel perfectly in the Preview window.
To disable the Snap to Pixel property, select the Text Block 2D node for which you want to disable that property and in the Properties add and disable the Snap to Pixel property.

When you want the background of the text block nodes to be transparent, you need to render the nodes in the scene graph of your Kanzi application in the correct order. See Rendering transparent nodes.

Adding text to your application

To add text to your application:

  1. In the Project press Alt and right-click the node where you want to show text and select either Text Block 3D, or Text Block 2D.
    Note that you can create a 3D node only inside 3D nodes, and 2D node only inside 2D nodes.
    TIP

    In the Preview use the Text Block 2D tool to click and drag to create a Text Block 2D node. To position the Text Block 2D node in a layout, click and drag that Text Block 2D node, press and hold Ctrl, and drop the node on top of the cell where you want to place it.

  2. In the Properties in the Text property enter the text you want to show. To add a new line press Shift+Enter.
    Use the Text Block nodes to show a small amount of text in your application. When the text is longer than a text block can show, by default Kanzi shows ellipsis. Add and set the Overflow property to show custom text.
  3. In the Properties set the text block properties to make the text look the way you want it to look.
    For example, set the Font Size property to define the size of the text.
  4. (Optional) In the Font property select the font you want to use.
    The default font in Kanzi is Fira Sans Regular. It is stored in <KanziInstallation>/Studio/Asset Library/Fonts. When you want to use your own font, import it to your Kanzi Studio project. See Importing fonts.
  5. (Optional) In the Properties add and set the Fixed Character Width property.
    Use the Fixed Character Width property to turn any font into a monospaced font.

Changing the size of text dynamically

To dynamically change the size of text in a Text Block node, use the Scale property field of either Render Transformation or Layout Transformation properties, instead of the Font Size property. For example, use this approach when you want to animate the size of text in a Text Block node.
When you use the Font Size property to dynamically scale the text, Kanzi creates multiple textures for different font sizes and does not release them from the memory.

Setting the color of text in a Text Block 2D node

To set the color of text in a Text Block 2D node:

  1. In the Project create or select a Text Block 2D node.
  2. In the Properties add the Foreground Brush property.
  3. Set the Foreground Brush property to an existing Color Brush or select + Color Brush, click next to the property, and set the color of the Color Brush you created.

Setting the color of text in a Text Block 3D node

To set the color of text in a Text Block 3D node:

  1. In the Project create or select a Text Block 3D node.
  2. In the Properties add and set the Font Color property.

Setting the appearance of a Text Block 2D node

To set the appearance of 2D nodes:

Using the Text Block 3D node in the API

To create a Text Block 3D node:

// Create a Text Block 3D node named Text block.
TextBlock3DSharedPtr textBlock = TextBlock3D::create(domain, "Text block");

To set the font of a text block:

// Set the font used by the text block node using the resource ID.
textBlock->setFont(font);

To add the text shown by a text block:

// Set the content of the text block node to read Hello world!.
textBlock->setText("Hello\nworld!");

To adjust the appearance of the text:

// Set the style of the text shown by the text block node.
// Set the size of the font to 90.0f.
textBlock->setFontSize(90.0f);
// Set the color of the font to blue.
textBlock->setFontColor(ThemeBlue);
// Align the text in the text block to the right.
textBlock->setTextHorizontalAlignment(TextBlockConcept::TextHorizontalAlignmentRight);

For details, see the TextBlock3D class in the API reference.

Using the Text Block 2D node in the API

To create a Text Block 2D node:

// Create a Text Block 2D node named Text node.
TextBlock2DSharedPtr textNode = TextBlock2D::create(domain, "Text node");

To set the font of a text block:

textNode->setFont(font);

To add the text shown by a text block:

// Set the content of the text block to read Hello world!.
textNode->setText("Hello\nworld!");

To adjust the appearance of the text:

// Set the style of the text shown by the text block.
// Set the line spacing to 1.5f.
textNode->setLineSpacing(1.5f);
// Set the size of the font to 36.0f.
textNode->setFontSize(36.0f);
// Set the color of the font to blue.
textNode->setFontColor(ThemeBlue);
// Align the text in the text block to the right.
textNode->setTextHorizontalAlignment(TextBlockConcept::TextHorizontalAlignmentRight);

For details, see the TextBlock2D class in the API reference.

See also

Glyph cache texture size

Importing fonts